﻿using System;
using System.Collections.Generic;
using System.Web.Security;
using FFIV.Lib.IView.Users;
using FFIV.Lib.Model;

namespace FFIV.Lib.Controlers.Admin
{
	public class ManageUsersController
	{
		private IManageUsers m_View;

		public ManageUsersController(IManageUsers view)
		{
			m_View = view;
			m_View.Controller = this;
		}

		public void LoadPage()
		{
			List<UserInfos> userInfos = new List<UserInfos>();

			MembershipUserCollection users = Membership.GetAllUsers();

			foreach (MembershipUser user in users)
			{
				UserInfos info = new UserInfos
								{
									UserId = new Guid(user.ProviderUserKey.ToString()),
									UserName = user.Comment,
									EMail = user.Email,
									CreateDate = user.CreationDate,
									LastLoginDate = user.LastLoginDate
								};
				userInfos.Add(info);
			}

			m_View.Users = userInfos;
		}

		public void UpdateUserInfo(Guid userId, string name)
		{
			MembershipUser user = Membership.GetUser(userId);
			if (user != null)
			{
				user.Comment = name;
				Membership.UpdateUser(user);
				
			}
		}

		public void DeleteUser(Guid userId)
		{
			Membership.DeleteUser(Membership.GetUser(userId).UserName);
		}

		public void ResetPassword(Guid userId)
		{
			MembershipUser user = Membership.GetUser(userId);
			if (user != null)
			{
				string oldPassword = user.ResetPassword();
				user.ChangePassword(oldPassword, "123456");
			}
		}
	}
}
